package sguigu.threee.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import sguigu.threee.entity.Address;
import sguigu.threee.entity.OrderEntity;
import sguigu.threee.entity.User;

import java.util.Date;
import java.util.UUID;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2022/12/21 14:03
 * @Version: 1.0
 */
@Slf4j
@RestController
public class RabbitMQController {

    @Autowired
    AmqpAdmin amqpAdmin;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMq")
    public String sendMq(){
        //1、发送消息,如果发送的消息是个对象，我们会使用序列化机制,将对象写出去。对象必须实现Serializablel
        String msg = "hello world";
        //2、发送的对象类型的消息，可以是一个json
        for (int i = 1; i <= 5; i++) {
                User user = new User();
                user.setUserId(1);
                user.setUserName("张三"+i);
                user.setPassword("111111");
                user.setAge(20);
                rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",user,new CorrelationData(UUID.randomUUID().toString()));
//                log.info("消息发送完成 {}",user);
        }
        return "ok";
    }

    @GetMapping("/sendMq2")
    public String sendMq2(){
        //1、发送消息,如果发送的消息是个对象，我们会使用序列化机制,将对象写出去。对象必须实现Serializablel
        String msg = "hello world";
        //2、发送的对象类型的消息，可以是一个json
        for (int i = 1; i <= 10; i++) {
            if (i%2==0){
                User user = new User();
                user.setUserId(1);
                user.setUserName("张三"+i);
                user.setPassword("111111");
                user.setAge(20);
                rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",user,new CorrelationData(UUID.randomUUID().toString()));
//                log.info("消息发送完成 {}",user);
            }else{
                Address address = new Address();
                address.setAddress("地址"+i);
                rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",address,new CorrelationData(UUID.randomUUID().toString()));
//                log.info("消息发送完成 {}",address);
            }
        }
        return "ok";

    }

    @ResponseBody
    @GetMapping("/test/createOrder")
    public String createOrderTest(){
        //订单下单成功
        OrderEntity entity = new OrderEntity();
        String orderId = UUID.randomUUID().toString();
        entity.setOrderSn(orderId);
        entity.setCreateTime(new Date());
        //发送到mq
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",entity);
        System.out.println("创建时间："+new Date()+"，订单编号："+orderId);
        return "ok";
    }

}
